package com.augurit.smartform.converter;

import java.util.ArrayList;
import java.util.List;

import com.augurit.smartform.entity.SmComponentDefinition;
import com.augurit.smartform.mgmt.SmartComponentTreeNode;
import com.augurit.smartform.web.component.form.ComponentDefinitionForm;

public class ComponentDefinitionConverter {
	public static ComponentDefinitionForm convertToForm(SmComponentDefinition entity){
		if(entity!=null){
			ComponentDefinitionForm form=new ComponentDefinitionForm();
			form.setId(entity.getId());
			form.setContent(entity.getContent());
			form.setIsView(entity.getIsView());
			form.setViewContent(entity.getViewContent());
			form.setTag(entity.getTag());
			form.setCreatePerson(entity.getCreatePerson());
			form.setCreateTime(entity.getCreateTime());
			form.setEditPerson(entity.getEditPerson());
			form.setEditTime(entity.getEditTime());
			form.setIcon(entity.getIcon());
			form.setAvailable(entity.getAvailable());
			form.setTagCn(entity.getTagCn());
			form.setRegisterId(entity.getRegisterId());
			return form;
		}else{
			return null;
		}
	}
	
	public static void convertToEntity(ComponentDefinitionForm form,SmComponentDefinition entity){
		if(entity!=null&&form!=null){
			entity.setId(form.getId());
			entity.setContent(form.getContent());
			entity.setIsView(form.getIsView());
			entity.setViewContent(form.getViewContent());
			entity.setTag(form.getTag());
			entity.setCreatePerson(form.getCreatePerson());
			entity.setCreateTime(form.getCreateTime());
			entity.setEditPerson(form.getEditPerson());
			entity.setEditTime(form.getEditTime());
			entity.setIcon(form.getIcon());
			entity.setAvailable(form.getAvailable());
			entity.setTagCn(form.getTagCn());
			entity.setRegisterId(form.getRegisterId());
		}
	}
	
	public static List<ComponentDefinitionForm> convertToFormList(List<SmComponentDefinition> list){
		if(list!=null&&list.size()>0){
			List<ComponentDefinitionForm> result=new ArrayList<ComponentDefinitionForm>();
			
			for(SmComponentDefinition entity : list){
				ComponentDefinitionForm form=convertToForm(entity);
				result.add(form);
			}
			
			return result;
		}else{
			return null;
		}
	}
	
	public static SmartComponentTreeNode convertComponentDefinitionToTreeNode(ComponentDefinitionForm form){
		if(form!=null){
			SmartComponentTreeNode node=new SmartComponentTreeNode();
			node.setId(SmartComponentTreeNode.getKey(SmComponentDefinition.class.getSimpleName(), form.getId()));
			node.setText(form.getTagCn());
			node.setQtip(form.getTag());
			node.setIconCls(SmartComponentTreeNode.CLS_APP);
			node.setLeaf(true);
			return node;
		}else{
			return null;
		}
	}
	
	public static List<SmartComponentTreeNode> convertComponentDefintionToTreeNode(List<ComponentDefinitionForm> list){
		List<SmartComponentTreeNode> result = new ArrayList<SmartComponentTreeNode>();
		if(list != null && list.size() > 0){
			for(ComponentDefinitionForm form : list)
				result.add(convertComponentDefinitionToTreeNode(form));
		}
		return result;
	}
}
